package com.zhulanhua.blogs.controller;


import com.zhulanhua.blogs.Utils.Code;
import com.zhulanhua.blogs.Utils.DozerUtils;
import com.zhulanhua.blogs.Utils.Result;
import com.zhulanhua.blogs.controller.params.BranchImagesParams;
import com.zhulanhua.blogs.controller.params.BranchParams;
import com.zhulanhua.blogs.domain.Branch;
import com.zhulanhua.blogs.domain.Image;
import com.zhulanhua.blogs.domain.query.AlbumQuery;
import com.zhulanhua.blogs.service.AlbumsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 相册接口
 * */
@RestController
@RequestMapping("/api/albums")
@CrossOrigin
public class AlbumsController {

    @Autowired
    AlbumsService albumsService;
    @Autowired
    DozerUtils dozerUtils;

    /**
     * 获取相册分类列表
     * */
    @GetMapping("/list")
    public Result<List<Branch>> getAlbums(){
        return new Result(Code.OK, albumsService.getAlbums());
    }
    /**
     * 获取该相册分类的照片列表
     * @param aid   相册id
     * @param page  第几页，默认为1
     * @param limit 一页多少个，默认为10
     * */
    @GetMapping("/{aid}")
    public Result<Branch<Image>> getAlbumsItem(@PathVariable("aid") long aid,
                                               @RequestParam(value = "page",defaultValue = "1") int page,
                                               @RequestParam(value = "limit",defaultValue = "10") int limit){
        return new Result(Code.OK, albumsService.getAlbumsItem(aid));
    }
    /**
     * 根据条件筛选相册照片列表
     * @param albumQuery
     * */
    @GetMapping("/query")
    public Result<Branch<Image>> getQueryItemPage(@Validated AlbumQuery albumQuery){
        return new Result(Code.OK, albumsService.getQueryItemPage(albumQuery));
    }
    /**
     * 新增相册
     * @param branchName
     * */
    @PostMapping("/add")
    public Result saveAlbums(@Validated @RequestBody BranchParams branchName){
        albumsService.saveAlbums(dozerUtils.convert(branchName,Branch.class));
        return Result.ok();
    }

    /**
     * 修改相册
     * @param branchName
     * */
    @PutMapping("/update")
    public Result updateAlbums(@Validated @RequestBody BranchParams branchName){
        albumsService.updateAlbums(dozerUtils.convert(branchName,Branch.class));
        return Result.ok();
    }

    /**
     * 删除相册
     * @param aid 相册id
     * */
    @DeleteMapping("/{aid}")
    public Result deleteAlbums(@PathVariable long aid){
        albumsService.deleteAlbums(aid);
        return Result.ok();
    }
    /**
     * 删除照片
     * @param imgs 照片id
     * */
    @DeleteMapping("/images/{imgs}")
    public Result deleteAlbumsItem(@PathVariable String imgs){
        albumsService.deleteAlbumsItem(imgs);
        return Result.ok();
    }
    /**
     * 相册照片上传
     * @param branchImagesParams
     * */
    @PostMapping("/images")
    public Result saveAlbumsItem(@Validated @RequestBody BranchImagesParams branchImagesParams){
        albumsService.saveAlbumsItem(branchImagesParams.getAid(), branchImagesParams.getList());
        return Result.ok();
    }
}
